define(function (require, exports, module) {
    var oBody = $('body');

    function getCookie(name) {
        var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
        if(arr=document.cookie.match(reg)) {
            return unescape(arr[2]);
        }
        else{
            return null;
        }

    }

    function  JudgeSource(ios, and, wxmin, wx, alipay, qq, h5) {
        var u = window.navigator.userAgent;
        //var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
        var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端

        var isWXMini = window.__wxjs_environment == 'miniprogram';

        var ua = u.toLowerCase();
        var isWeiXin = ua.match(/MicroMessenger/i) == 'micromessenger';
        var isAlipay = ua.match(/AlipayClient/i) == 'alipayclient';
        var isQQ = ua.match(/QQ/i) == 'qq';

        // 检测室安卓还是ios
        if(u.search('origin/sfddjapp') > 0) {
            if(isIOS) { if(ios) ios(); }
            else { if(and) and(); }
        }

        // 检测小程序
        else if(isWXMini) { if(wxmin) wxmin(); }

        // 检测ddj wx, alipay, qq
        else {
            if(isWeiXin) { if(wx) wx(); }
            else if(isAlipay) { if(alipay) alipay(); }
            else if(isQQ) { if(qq) qq(); }
            else { if(h5) h5(); }
        }
    }

    // 第三方统计的插件
    var TPLMethods = {
        userInfo: JSON.parse( getCookie( 'ddj-userInfo') ),

        // 百度统计
        baidu: function() {
            /**
             *  百度统计
             */
            var sfddjUrl = '9a6219852c6bc360b517de9714e19810'; //m.sfddj.com
            (function () {
                var hm = document.createElement("script");
                hm.src = "//hm.baidu.com/hm.js?" + sfddjUrl;
                // var s = document.getElementsByTagName("script")[0];
                // s.parentNode.insertBefore(hm, s);
                oBody.append(hm);
            })();
        },

        // gio 统计
        gio: function() {
            var _this = this;
            !function (e, t, n, g, i) {
                e[i] = e[i] || function () {
                    (e[i].q = e[i].q || []).push(arguments)
                },
                    n = t.createElement("script");
                // tag = t.getElementsByTagName("script")[0];
                // n.async = 1;
                n.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + g;
                // tag.parentNode.insertBefore(n, tag)
                oBody.append(n);
            }(window, document, "script", "assets.growingio.com/2.1/gio.js", "gio");

            var _gioSetUserId = '';
            if (_this.userInfo) {
                if( _this.userInfo.memberId ) {
                    _gioSetUserId = _this.userInfo.memberId;
                }
                else if( _this.userInfo.openId ) {
                    _gioSetUserId = _this.userInfo.openId;
                }
                else {
                    _gioSetUserId = '';
                }
            }

            gio('init', 'b665fa1721dac33b', {});
            // console.log(ddjhoc.userAction)
            // console.log(gio)
            // 给gio 传递用户信息
            gio('setUserId', _gioSetUserId);
            gio('send');
        },
    };


    // ZaneMa Testing
    $.getScript("/static_v4/js/plug/DDJStatistics.js", function(response,status) {
        ddjhoc.init(function () {
            // TPLMethods.baidu();
            TPLMethods.gio();
        });
    });
});



